package com.njworkorder.Controller;


import com.njworkorder.HTSingleLogin.AjaxResult;
import com.njworkorder.HTSingleLogin.FileInfo;
import com.njworkorder.Service.FileInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;

/**
 * 青云oss上传
 *
 * @author ht_lzc
 * @date 2024/12/20 14:19
 */
@Slf4j
@RestController
@RequestMapping("/qy/oss")
@Tag(name = "青云oss对象存储", description = "青云oss对象存储")
public class QyOssController{

    @Value("${qy.oss.bucket_name}")
    private String qyBucketName;
    @Value("${qy.oss.zone_name}")
    private String qyZoneName;
    @Value("${qy.oss.access_key_id}")
    private String accessKeyId;
    @Value("${qy.oss.secret_access_key}")
    private String accessKey;

    private final FileInfoService fileInfoService;
    public QyOssController(FileInfoService fileInfoService) {
        this.fileInfoService = fileInfoService;
    }

    @Operation(description = "青云oss后端上传单个或多个文件",summary = "青云oss后端上传单个或多个文件")
    @PostMapping("/upload")
    public AjaxResult upload(@RequestParam(value = "files") MultipartFile[] files){
        return  AjaxResult.success(fileInfoService.uploadFiles(files));
    }

    @Operation(description = "青云oss后端上传单个或多个base64图片",summary = "青云oss后端上传单个或多个base64图片")
    @PostMapping("/upload/base64")
    public AjaxResult uploadBase64(@RequestBody List<String> files){
        return  AjaxResult.success(fileInfoService.uploadBase64(files));
    }


    @Operation(summary = "后端单个或多个删除文件",description = "后端单个或多个删除文件")
    @DeleteMapping("/delete/{ids}")
    public AjaxResult delete(@PathVariable("ids") List<String> ids)
    {
        return toAjax(fileInfoService.deleteQyFileInfoByIds(ids));
    }


    @Operation(summary = "获取oss的加密后的key与id",description = "获取oss的加密后的key与id")
    @GetMapping("/sign")
    public AjaxResult getSignBody()
    {
        HashMap<String, String> data = new HashMap<>();
        data.put("bucketName",qyBucketName);
        data.put("zoneName",qyZoneName);
        data.put("accessKeyId",accessKeyId);
        data.put("accessKeySecret",accessKey);
        String encode = Base64.getEncoder().encodeToString(data.toString().getBytes(StandardCharsets.UTF_8));
        return AjaxResult.success(encode);
    }


    @Operation(summary = "oss上传完成将信息传到后端入库",description = "oss上传完成将信息传到后端入库")
    @PostMapping("/insert/fileInfo")
    public AjaxResult insertFileInfo(@RequestBody FileInfo fileInfo) {

        int i = fileInfoService.insertFileInfo(fileInfo);
        if (i > 0) {
            return AjaxResult.success(fileInfo);
        }
        return toAjax(i);
    }


    @Operation(summary = "多个文件压缩包下载或单个文件普通下载",description = "多个文件压缩包下载或单个文件普通下载")
    @GetMapping("/download/zip")
    public void downloadZip(@RequestParam(value = "ids") List<Integer> ids, HttpServletResponse response) throws IOException {
        fileInfoService.downloadBatchFile(ids,response);
    }

    private AjaxResult toAjax(int rows)
    {
        return rows > 0 ? AjaxResult.success() : AjaxResult.error();
    }
}
